# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.views.generic.list_detail import object_list, object_detail
from django.views.generic.create_update import create_object, delete_object, \
    update_object
from google.appengine.ext import db
from mimetypes import guess_type
from jobs.forms import CompanyForm, JobForm
from jobs.models import Job, Company
from jobs.data_access import *
from ragendja.dbutils import get_object_or_404
from ragendja.template import render_to_response
import logging


def list_jobs(request):
    jobs = get_open_jobs()
    # find out whether current user is an employer
    if request.user.is_authenticated():
        company = get_user_company(request.user)
        if company:
            enable_add_job = True
    return object_list(request, jobs, extra_context=locals(), paginate_by=10)

def show_job(request, key):
    return object_detail(request, Job.all(), key)

@login_required
def add_job(request): 
    return create_object(request, form_class=JobForm,
        post_save_redirect=reverse('jobs.views.show_job',
                                   kwargs=dict(key='%(key)s')))

@login_required
def edit_job(request, key):
    return update_object(request, object_id=key, form_class=JobForm)

@login_required
def delete_job(request, key):
    return delete_object(request, Job, object_id=key,
        post_delete_redirect=reverse('jobs.views.list_jobs'))
    
@login_required
def add_and_associate_company(request):
    if request.method == "GET":
        form = CompanyForm()
        return render_to_response(request, 'jobs/company_form.html', locals())
    else:
        company = Company(name=request.POST["name"], email=request.POST["email"])
        company.phone = request.POST["phone"]
        company.location = request.POST["location"]
        company.description = request.POST["description"]
        if request.POST["url"] != "":
            company.url = request.POST["url"]
        company.twitter = request.POST["twitter"]
        company.save()
        formset = JobForm(request.POST)
        
        for i in range(int(request.POST["jobs-TOTAL_FORMS"])):
            if request.POST["jobs-%d-name" % i] != "":
                job = Job(name=request.POST["jobs-%d-name" % i], company=company)
                description = request.POST["jobs-%d-description" % i]
                job.save()
        employment = Employment(company=company, employee=request.user, work_email=company.email)
        employment.save()
        return HttpResponseRedirect(reverse('jobs.views.list_jobs'))

def create_admin_user(request):
    user = User.get_by_key_name('admin')
    if not user or user.username != 'admin' or not (user.is_active and
            user.is_staff and user.is_superuser and
            user.check_password('admin')):
        user = User(key_name='admin', username='admin',
            email='admin@localhost', first_name='Reversim', last_name='Admin',
            is_active=True, is_staff=True, is_superuser=True)
        user.set_password('admin')
        user.put()
    return render_to_response(request, 'jobs/admin_created.html')


